package com.harmontronics.hanma.paas.paas.domain.iam.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmPermActionFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmPermActionRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmPermAction;
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmPermActionMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmPermActionPO;
import java.util.List;

@Service("hmPermActionService")
@DS("#header.App-Unique")
public class HmPermActionService{

    @Autowired
    private HmPermActionRepository hmPermActionRepository;

    @Autowired
    private HmPermActionMapper hmPermActionMapper;


    public HmPermAction findById( Integer id){
            HmPermActionPO po =   hmPermActionRepository.getById(id);
        return BeanUtil.toBean(po,HmPermAction.class);
    }

    public void save(HmPermAction hmPermAction){
            HmPermActionPO po =  BeanUtil.toBean(hmPermAction,HmPermActionPO.class);
            hmPermActionRepository.save(po);
    }

    public void create(HmPermActionPO permAction) {
        hmPermActionRepository.save(permAction);
    }

    public void update(HmPermAction hmPermAction){
            HmPermActionPO po =  BeanUtil.toBean(hmPermAction,HmPermActionPO.class);
            hmPermActionRepository.updateById(po);
    }

    public void remove(List<Integer> ids){
            hmPermActionRepository.removeByIds(ids);
    }

    public List<HmPermAction> getList(){
        List<HmPermActionPO> pos =  hmPermActionRepository.list();
        return Convert.toList(HmPermAction.class,pos) ;
    }

    public PageBase<HmPermAction> getList(int pageNo,int pageSize){
        Page<HmPermActionPO> page = new Page<>(pageNo, pageSize);
        IPage<HmPermActionPO> resultPage =
                    hmPermActionRepository.selectPage(Wrappers.emptyWrapper(),page);
        return PageBase.from(resultPage,HmPermAction.class);
    }


    public List<HmPermActionPO> getPermActionsByPermIds(List<Long> permIds) {
        QueryWrapper<HmPermActionPO> queryWrapper = new QueryWrapper<HmPermActionPO>().
                in(HmPermActionFields.PERM_ID, permIds).
                eq(HmPermActionFields.DELETED, NumberEnum.ZERO.getNum());
        return  hmPermActionMapper.selectList(queryWrapper);
    }

    public void updateBatchById(List<HmPermActionPO> poList){
        hmPermActionRepository.updateBatchById(poList);
    }

    /**
     * 根据权限id 和 操作id 更新
     *
     * @param permId
     * @param actionIds
     */
    public void updateWithPermId(Long permId, List<Long> actionIds) {
        // 1. 删除原有的数据
        hmPermActionMapper.delete(new UpdateWrapper<HmPermActionPO>().eq(HmPermActionFields.PERM_ID, permId));

        if (CollectionUtils.isNotEmpty(actionIds)) {
            // 2.重新写入现有的操作
            List<HmPermActionPO> permActionList = new ArrayList<>();
            for (Long id : actionIds) {
                permActionList.add(new HmPermActionPO().setPermId(permId).setActionId(id).setDeleted(Integer.valueOf(0)));
            }
            hmPermActionRepository.saveBatch(permActionList);
        }
    }

    public List<HmPermActionPO> getPermActions(List<Long> actionIds){
        QueryWrapper<HmPermActionPO> queryWrapper = new QueryWrapper<HmPermActionPO>().
                in(HmPermActionFields.ACTION_ID, actionIds).
                eq(HmPermActionFields.DELETED, NumberEnum.ZERO.getNum());
        return hmPermActionRepository.list(queryWrapper);
    }

}